﻿using Lamex.Base;
using Lamex.Interface;
using Lamex.Library;
using Lamex.Library.CommonClass;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Lamex.mDashboard.UI
{
    public partial class FrmTableBarcodeTime : frmBaseChild
    {
        public FrmTableBarcodeTime()
        {
            InitializeComponent();
            GetInitialize();
        }

        private void FrmTableBarcodeTime_Load(object sender, EventArgs e)
        {
            try { this.InitButtons(); } catch { }//初始化标准功能，因要应用于RibbonForm，所以加入异常处理
            gridControl1.UseEmbeddedNavigator = true;
        }
        public DataTable GetJT()
        {
            DataTable dt = new DataTable();
            try
            {
                string sql = @" SELECT DISTINCT [Machine_Name],IDNO from [Edgeband_Program].[dbo].[Machine_Name] order by IDNO";
                dt = BaseBuildSql.GetDataTableBySql(sql);
            }
            catch
            {
            }
            return dt;

        }
        private void GetInitialize()
        {
            DataTable dt = GetJT();
            ccbeJT.Properties.DataSource = dt;
            ccbeJT.Properties.DisplayMember = "Machine_Name";
            ccbeJT.Properties.ValueMember = "Machine_Name";

            this.dtStartTime.Properties.DisplayFormat.FormatString = "yyyy-MM-dd";
            this.dtStartTime.Properties.DisplayFormat.FormatType = DevExpress.Utils.FormatType.DateTime;
            this.dtStartTime.Properties.EditFormat.FormatString = "yyyy-MM-dd";
            this.dtStartTime.Properties.EditFormat.FormatType = DevExpress.Utils.FormatType.DateTime;
            this.dtStartTime.Properties.Mask.EditMask = "yyyy-MM-dd";

            this.dtEndTime.Properties.DisplayFormat.FormatString = "yyyy-MM-dd";
            this.dtEndTime.Properties.DisplayFormat.FormatType = DevExpress.Utils.FormatType.DateTime;
            this.dtEndTime.Properties.EditFormat.FormatString = "yyyy-MM-dd";
            this.dtEndTime.Properties.EditFormat.FormatType = DevExpress.Utils.FormatType.DateTime;
            this.dtEndTime.Properties.Mask.EditMask = "yyyy-MM-dd";

            dtStartTime.Text = DateTime.Now.AddDays(-6).ToString("yyyy-MM-dd");
            dtEndTime.Text = DateTime.Now.AddDays(1).ToString("yyyy-MM-dd");
        }
        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="sender"></param>
        public override void DoView(Lamex.Interface.IButtonInfo sender)
        {
            //using (DevExpress.Utils.WaitDialogForm wdf = new DevExpress.Utils.WaitDialogForm("请稍等", "正在查询数据......", new Size(400, 80)))
            //{
            try
            {
                SplashScreenTool.ShowSplashScreen(typeof(WaitForm1));
                if (string.IsNullOrEmpty(dtStartTime.Text))
                {
                    DevExpress.XtraEditors.XtraMessageBox.Show("请选择开始时间", "提示", MessageBoxButtons.OK);
                    return;
                }
                if (string.IsNullOrEmpty(dtEndTime.Text))
                {
                    DevExpress.XtraEditors.XtraMessageBox.Show("请选择结束时间", "提示", MessageBoxButtons.OK);
                    return;
                }
                string strPO = string.IsNullOrEmpty(txtPo.Text) ? "and OPTIMISATION_RUN_NO like '%%'" : "and OPTIMISATION_RUN_NO like '%" + txtPo.Text + "%'";
                string sqlJT = "";
                #region 组合
                switch (ccbeJT.Text)
                {
                    case "LS1":
                        sqlJT = ",[LS1]";
                        break;
                    case "FH4":
                        sqlJT = ",[FH4]";
                        break;
                    case "BIMA":
                        sqlJT = ",[BIMA]";
                        break;
                    case "COMBIMA":
                        sqlJT = @" ,[InCombima] ,[CombimaPass1],[CombimaPass2],[CombimaPass3],[CombimaPass4] ,[OutCombima]";
                        break;
                    case "PH":
                        sqlJT = @",PuHA,PuHB,[AfterPuH]";
                        break;
                    case "LS1, FH4":
                        sqlJT = ",[LS1],[FH4]";
                        break;
                    case "LS1, PH":
                        sqlJT = ",[LS1],PuHA,PuHB,[AfterPuH]";
                        break;
                    case "LS1, COMBIMA":
                        sqlJT = ",[LS1],[InCombima] ,[CombimaPass1],[CombimaPass2],[CombimaPass3],[CombimaPass4] ,[OutCombima]";
                        break;
                    case "LS1, FH4, BIMA":
                        sqlJT = ",[LS1],[FH4],BIMA";
                        break;
                    case "LS1, FH4, COMBIMA":
                        sqlJT = ",[LS1],[FH4],[InCombima] ,[CombimaPass1],[CombimaPass2],[CombimaPass3],[CombimaPass4] ,[OutCombima]";
                        break;
                    case "LS1, FH4, COMBIMA, PH":
                        sqlJT = ",[LS1],[FH4],[InCombima] ,[CombimaPass1],[CombimaPass2],[CombimaPass3],[CombimaPass4] ,[OutCombima],PuHA,PuHB,[AfterPuH]";
                        break;
                    case "LS1, FH4, COMBIMA, BIMA":
                        sqlJT = ",[LS1],[FH4],BIMA,[InCombima] ,[CombimaPass1],[CombimaPass2],[CombimaPass3],[CombimaPass4] ,[OutCombima]";
                        break;
                    case "FH4, BIMA":
                        sqlJT = ",[FH4],BIMA";
                        break;
                    case "FH4, PH":
                        sqlJT = ",[FH4],PH";
                        break;
                    case "FH4, COMBIMA":
                        sqlJT = ",[FH4],[InCombima] ,[CombimaPass1],[CombimaPass2],[CombimaPass3],[CombimaPass4] ,[OutCombima]";
                        break;
                    case "FH4, COMBIMA, BIMA":
                        sqlJT = ",[FH4],BIMA,[InCombima] ,[CombimaPass1],[CombimaPass2],[CombimaPass3],[CombimaPass4] ,[OutCombima]";
                        break;
                    case "FH4, COMBIMA, PH":
                        sqlJT = ",[FH4],[InCombima] ,[CombimaPass1],[CombimaPass2],[CombimaPass3],[CombimaPass4] ,[OutCombima],PuHA,PuHB,[AfterPuH]";
                        break;
                    case "FH4, COMBIMA, PH, BIMA":
                        sqlJT = ",[FH4],BIMA,[InCombima] ,[CombimaPass1],[CombimaPass2],[CombimaPass3],[CombimaPass4] ,[OutCombima],PuHA,PuHB,[AfterPuH]";
                        break;
                    case "COMBIMA, BIMA":
                        sqlJT = ",BIMA,[InCombima] ,[CombimaPass1],[CombimaPass2],[CombimaPass3],[CombimaPass4] ,[OutCombima]";
                        break;
                    case "COMBIMA, PH, BIMA":
                        sqlJT = ",BIMA,[InCombima] ,[CombimaPass1],[CombimaPass2],[CombimaPass3],[CombimaPass4] ,[OutCombima],PuHA,PuHB,[AfterPuH]";
                        break;
                    case "COMBIMA, PH":
                        sqlJT = ",[InCombima] ,[CombimaPass1],[CombimaPass2],[CombimaPass3],[CombimaPass4] ,[OutCombima],PuHA,PuHB,[AfterPuH]";
                        break;
                    default:
                        sqlJT = @",[LS1]
      ,[FH4]
      ,[BIMA]
      ,[InCombima]
      ,[CombimaPass1]
      ,[CombimaPass2]
      ,[CombimaPass3]
      ,[CombimaPass4]
      ,[OutCombima]
      ,[PuHA]
      ,[PuHB]
      ,[AfterPuH]";
                        break;
                }
                #endregion
                string sql = @"SELECT  [PART_BARCODE]
      ,[PART_NO]
      ,[PACKAGE_MO]
      ,[ORDER_NUMBER]
      ,[OPTIMISATION_RUN_NO]
      ,[Planning]
      ,[Planed]
      ,[QUEUE]
      ,[Optimized]
      ,[Released]     
      " + sqlJT + "  ,[Completed] FROM [Edgeband_Program].[dbo].[Barcode_Time] where Planning>='" + dtStartTime.Text + "' and Planning<='" + dtEndTime.Text + "' ";
                sql = sql + strPO;
                gridView1.Columns.Clear();
                DataTable dt = BaseBuildSql.GetDataTableBySql(sql);
                gridControl1.DataSource = dt;

                gridView1.Columns["Planning"].DisplayFormat.FormatString = "yyyy-MM-dd HH:mm:ss";
                gridView1.Columns["Planed"].DisplayFormat.FormatString = "yyyy-MM-dd HH:mm:ss";
                gridView1.Columns["QUEUE"].DisplayFormat.FormatString = "yyyy-MM-dd HH:mm:ss";
                gridView1.Columns["Optimized"].DisplayFormat.FormatString = "yyyy-MM-dd HH:mm:ss";
                gridView1.Columns["Released"].DisplayFormat.FormatString = "yyyy-MM-dd HH:mm:ss";
                gridView1.Columns["Completed"].DisplayFormat.FormatString = "yyyy-MM-dd HH:mm:ss";
                if (gridView1.Columns["LS1"] != null)
                    gridView1.Columns["LS1"].DisplayFormat.FormatString = "yyyy-MM-dd HH:mm:ss";
                if (gridView1.Columns["FH4"] != null)
                    gridView1.Columns["FH4"].DisplayFormat.FormatString = "yyyy-MM-dd HH:mm:ss";
                if (gridView1.Columns["BIMA"] != null)
                    gridView1.Columns["BIMA"].DisplayFormat.FormatString = "yyyy-MM-dd HH:mm:ss";
                if (gridView1.Columns["InCombima"] != null)
                    gridView1.Columns["InCombima"].DisplayFormat.FormatString = "yyyy-MM-dd HH:mm:ss";
                if (gridView1.Columns["CombimaPass1"] != null)
                    gridView1.Columns["CombimaPass1"].DisplayFormat.FormatString = "yyyy-MM-dd HH:mm:ss";
                if (gridView1.Columns["CombimaPass2"] != null)
                    gridView1.Columns["CombimaPass2"].DisplayFormat.FormatString = "yyyy-MM-dd HH:mm:ss";
                if (gridView1.Columns["CombimaPass3"] != null)
                    gridView1.Columns["CombimaPass3"].DisplayFormat.FormatString = "yyyy-MM-dd HH:mm:ss";
                if (gridView1.Columns["CombimaPass4"] != null)
                    gridView1.Columns["CombimaPass4"].DisplayFormat.FormatString = "yyyy-MM-dd HH:mm:ss";
                if (gridView1.Columns["OutCombima"] != null)
                    gridView1.Columns["OutCombima"].DisplayFormat.FormatString = "yyyy-MM-dd HH:mm:ss";
                if (gridView1.Columns["PuHA"] != null)
                    gridView1.Columns["PuHA"].DisplayFormat.FormatString = "yyyy-MM-dd HH:mm:ss";
                if (gridView1.Columns["PuHB"] != null)
                    gridView1.Columns["PuHB"].DisplayFormat.FormatString = "yyyy-MM-dd HH:mm:ss";
                if (gridView1.Columns["AfterPuH"] != null)
                    gridView1.Columns["AfterPuH"].DisplayFormat.FormatString = "yyyy-MM-dd HH:mm:ss";

                gridView1.BestFitColumns();
                this.gridView1.OptionsView.ColumnAutoWidth = false;
                base.DoView(sender);
                SplashScreenTool.CloseSplashScreen();

            }
            catch (Exception ex)
            {
                SplashScreenTool.CloseSplashScreen();
            }

            //}
        }

        //导出
        public override void DoExport(IButtonInfo sender)
        {
            try
            {
                SaveFileDialog dlg = new SaveFileDialog();
                //dlg.Filter = "PDF file(*.pdf)|*.pdf";
                dlg.Filter = "Excel文件(*.xls)|*.xls";
                DialogResult dialogResult = dlg.ShowDialog(this);
                if (dialogResult == DialogResult.OK)
                {
                    gridView1.ExportToXls(dlg.FileName);
                    DevExpress.XtraEditors.XtraMessageBox.Show("导出成功", "提示", MessageBoxButtons.OK);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
    }
}
